package com.fourforfo.fourmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fourforfo.common.exception.BizCodeEnum;
import com.fourforfo.fourmall.product.entity.ProductAttrValueEntity;
import com.fourforfo.fourmall.product.service.ProductAttrValueService;
import com.fourforfo.fourmall.product.vo.AttrRespVO;
import com.fourforfo.fourmall.product.vo.AttrVO;
import javafx.scene.chart.ScatterChart;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.fourforfo.fourmall.product.entity.AttrEntity;
import com.fourforfo.fourmall.product.service.AttrService;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.R;



/**
 * 商品属性
 *
 * @author fourforfo
 * @email fourforfo@gmail.com
 * @date 2022-06-16 12:30:14
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 修改商品规格
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@RequestBody List<ProductAttrValueEntity> entities,@PathVariable Long spuId){
        if(entities!=null&&entities.size()>0&&spuId!=null&&spuId!=0) {

            productAttrValueService.updateBatchAttrValue(entities,spuId);
        }

        return R.ok();
    }

    /**
     * 根据spuId获取spu参数
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")Long spuId){
        if(spuId==null) return R.error(BizCodeEnum.ATTR_TYPE_EXCEPTION.getCode(),BizCodeEnum.ATTR_TYPE_EXCEPTION.getMsg()+"：spuId不能为空！");

        List<ProductAttrValueEntity> productAttrValueEntities= productAttrValueService.listBySpuId(spuId);

        return R.ok().put("data", productAttrValueEntities);
    }


    /**
     * 列表
     * 获取分类规格参数
     *  product/attr/base/list/{catelogId}
     *
     * 获取分类规格参数
     *  product/attr/sale/list/{catelogId}
     *
     */
    @GetMapping("/{attrType}/list/{catelogId}")
//    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("attrType")String attrType,@PathVariable("catelogId")Long catelogId){
        PageUtils page ;
        try{
            page = attrService.queryByTypeAndCatelog(params,attrType,catelogId);
        }catch (Exception e){
            return R.error(BizCodeEnum.ATTR_TYPE_EXCEPTION.getCode(),BizCodeEnum.ATTR_TYPE_EXCEPTION.getMsg()).put("error",e.getMessage());
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
//    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVO attr = attrService.getAttrVOById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr){
        //不提供属性更改功能，由于两者的属性不一样，由base修改成sale后不知道是否删除掉属性和属性分组关联表中有关base属性和分组的所有关系
		if(!attrService.updateAttr(attr)){
		    return R.error(BizCodeEnum.UNREALIZED_FUNCTION.getCode(), BizCodeEnum.UNREALIZED_FUNCTION.getMsg()).put("属性类型不能修改","涉及到的业务操作混乱，一开始就不该提供属性类型修改");
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody List<Long> attrIds){
        //删除所有attr以及attrattrGroupRelation信息
		attrService.removeAttrAndAttrAttrGroupRelation(attrIds);

        return R.ok();
    }

}
